//
//  XFYAnimationTool.m
//  SDKSample
//
//  Created by 小肥羊 on 15/11/17.
//  Copyright © 2015年 Zhu‘s Game House. All rights reserved.
//

#import "XFYAnimationTool.h"

#define DURATION 0.7f

@implementation XFYAnimationTool
#pragma mark - 单例
+ (XFYAnimationTool *)sharedAnimationTool
{
    static XFYAnimationTool *animationTool = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        animationTool = [[self alloc] init];
    });
    return animationTool;
}

#pragma mark - CATransition动画实现
- (void)animationWithView:(UIView *)view Type:(AnimationType)type FromType:(NSString *)fromType
{
    [self animationWithView:view Type:type FromType:fromType PlayTime:DURATION];
}
- (void)animationWithView:(UIView *)view Type:(AnimationType)type FromType:(NSString *)fromType PlayTime:(NSTimeInterval)playTime
{
    //创建CATransition对象
    CATransition *animation = [CATransition animation];
    //设置运动时间
    animation.duration = playTime;
    //设置运动type      kCATransitionFromLeft       kCATransitionFromBottom     kCATransitionFromRight      kCATransitionFromTop
    animation.type = [self changeToTransition:type];
    if (fromType != nil) {
        animation.subtype = fromType;   //设置子类
    }
    //设置运动速度
    animation.timingFunction = UIViewAnimationOptionCurveEaseInOut;
    [view.layer addAnimation:animation forKey:@"animation"];
}

#pragma UIView实现动画
- (void) animationWithView:(UIView *)view AnimationTransition:(UIViewAnimationTransition)transition
{
    [self animationWithView:view AnimationTransition:transition PlayTime:DURATION DelayTime:0.0];
}
- (void)animationWithView:(UIView *)view AnimationTransition:(UIViewAnimationTransition)transition PlayTime:(NSTimeInterval)playTime DelayTime:(NSTimeInterval)delayTime
{
    [UIView animateWithDuration:playTime animations:^{
        [UIView setAnimationDelay:delayTime];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationTransition:transition forView:view cache:YES];
    }];
//    //commitAnimations方式  用于转场
//    [UIView beginAnimations:@"animation" context:nil];      //开始一个动画块，第一个参数为动画块标识
//    [UIView setAnimationDelay:delayTime];                   //延时
//    [UIView setAnimationDuration:playTime];                 //设置动画的持续时间
//    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];   //设置动画块中的动画属性变化的曲线
//    [UIView setAnimationRepeatAutoreverses:NO];             //设置是否自动反转当前的动画效果
//    [UIView setAnimationTransition:transition forView:view cache:YES]; //设置过渡的动画效果
//    //    [self.view exchangeSubviewAtIndex:1 withSubviewAtIndex:0];    //页面翻转
//    [UIView commitAnimations];  //提交动画
}

#pragma mark - 制定剧本
- (CAAnimationGroup *)xfyAnimationWithView:(UIView *)view
{
    //http://www.cnblogs.com/wengzilin/p/4250957.html
    //设定剧本
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:1.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:1.2];
    scaleAnimation.autoreverses = YES;
//    scaleAnimation.repeatCount = MAXFLOAT;
    scaleAnimation.duration = 0.8;
    
    CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimation.fromValue = [NSValue valueWithCGPoint:view.layer.position];
    moveAnimation.toValue = [NSValue valueWithCGPoint:CGPointMake(320 - 80,
                                                                  view.layer.position.y)];
    moveAnimation.autoreverses = YES;
//    moveAnimation.repeatCount = MAXFLOAT;
    moveAnimation.duration = 2;
    
    CABasicAnimation *rotateAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.x"];
    rotateAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    rotateAnimation.toValue = [NSNumber numberWithFloat:6.0 * M_PI];
    rotateAnimation.autoreverses = YES;
//    rotateAnimation.repeatCount = MAXFLOAT;
    rotateAnimation.duration = 2;
    
    CAAnimationGroup *groupAnnimation = [CAAnimationGroup animation];
    groupAnnimation.duration = 2;
    groupAnnimation.autoreverses = YES;
    groupAnnimation.animations = @[moveAnimation, scaleAnimation, rotateAnimation];
//    groupAnnimation.repeatCount = MAXFLOAT;
    return groupAnnimation;
}

/*! @brief 展开 */
- (CAAnimationGroup *)openAnimationWithView:(UIView *)view
{
    //设定剧本
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale.y"];
    scaleAnimation.fromValue = [NSNumber numberWithFloat:0.0];
    scaleAnimation.toValue = [NSNumber numberWithFloat:1.0];
    
    CABasicAnimation *positionAnimation = [CABasicAnimation animationWithKeyPath:@"transform.translation.y"];
    [positionAnimation setFromValue:[NSNumber numberWithFloat:-view.bounds.size.height/2]];
    [positionAnimation setToValue:[NSNumber numberWithFloat:0.0]];
    
    CAAnimationGroup *groupAnnimation = [CAAnimationGroup animation];
    [groupAnnimation setAnimations:@[scaleAnimation, positionAnimation]];
//    groupAnnimation.autoreverses = YES;   重复
    return groupAnnimation;
}
- (CAAnimationGroup *)pushAnimationWithView:(UIView *)view
{
    CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    [scaleAnimation setFromValue:@0.8];
    [scaleAnimation setToValue:@1.0];
    
    CABasicAnimation *opacityAnimation = [CABasicAnimation animationWithKeyPath:@"opacity"];
    [opacityAnimation setFromValue:@0.0];
    [opacityAnimation setToValue:@1.0];
    
    CAAnimationGroup *group = [CAAnimationGroup animation];
    [group setAnimations:@[scaleAnimation, opacityAnimation]];
    [group setRemovedOnCompletion:NO];
    [group setFillMode:kCAFillModeBoth];
    return group;
}

#pragma mark - 内部转换
- (NSString *)changeToTransition:(AnimationType)transition
{
    switch (transition) {
        case XFYAnimationTypeFade:
            return kCATransitionFade;
        case XFYAnimationTypePush:
            return kCATransitionPush;
        case XFYAnimationTypeReveal:
            return kCATransitionReveal;
        case XFYAnimationTypeMoveIn:
            return kCATransitionMoveIn;
        case XFYAnimationTypeCube:
            return @"cube";
        case XFYAnimationTypeSuckEffect:
            return @"suckEffect";
        case XFYAnimationTypeOglFlip:
            return @"oglFlip";
        case XFYAnimationTypeRippleEffect:
            return @"rippleEffect";
        case XFYAnimationTypePageCurl:
            return @"pageCurl";
        case XFYAnimationTypePageUnCurl:
            return @"pageUnCurl";
        case XFYAnimationTypeCameraIrisHollowOpen:
            return @"cameraIrisHollowOpen";
        case XFYAnimationTypeCameraIrisHollowClose:
            return @"cameraIrisHollowClose";
        default:
            return kCATransitionFade;
    }
}

@end
